package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView mv =new ModelAndView();

        mv.addObject("roles",roleService.listAll());
        mv.setViewName("/role/list");
        return mv;
    }
    @RequestMapping("/input")
    public ModelAndView input(Long id){
        ModelAndView mv=new ModelAndView();
        if(id!=null){
            mv.addObject("role",roleService.selectById(id));

            List<Permission> rolePermission=roleService.listAllPermission(id);
            mv.addObject("rolePermission",rolePermission);
        }

        mv.addObject("permissionList",permissionService.selectAll());
        mv.setViewName("/role/input");
        return mv;
    }

    @RequestMapping("/saveOrUpdate")
    public ModelAndView saveOrUpdate(Role role,Long[] ids){
        ModelAndView mv=new ModelAndView();
        if(role.getId()==null){
            roleService.save(role,ids);

        }else {
            roleService.updateById(role,ids);
        }

        mv=list();
        return mv;
    }

}
